//
//  AppDelegate.swift
//  XMWeibo-Swift
//
//  Created by 育鑫 王 on 15/10/26.
//  Copyright © 2015年 育鑫 王. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    

    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        print("启动 account:\(XMUserAccount.loadAccount())")
//        print("启动 account:\(XMUserAccount.loadAccount())")
        
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        setupAppearance()
        
        //?:如果?前面的变量有值会执行后面的代码
//        window?.rootViewController = tabbar
        window?.rootViewController = defaultController()
        
        //为主窗口并显示
        window?.makeKeyAndVisible()
        
        return true
    }
    
    private func defaultController() -> UIViewController {
        //判断是否登录
        //每次判断都需要 == nil
        if !XMUserAccount.userLogin() {
            return XMMainViewController()
        }
        
        return isNewVersion() ? XMNewFeatureViewController() : XMWelcomeViewController()
    }
    
    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取当前的版本号
        let versionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as!String
        let currentVersion = Double(versionString)!
        print("currentVersion:\(currentVersion)")
        
        // 获取之前的版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        print("sandboxVersion\(sandboxVersion)")
        
        // 保存当前版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: "sandboxVersionKey")
        NSUserDefaults.standardUserDefaults().synchronize()
        
        
        return currentVersion > sandboxVersion
    }
    
    /**
    切换根控制器
    */
    func switchRootController(isMain:Bool) {
        window?.rootViewController = isMain ? XMMainViewController() : XMWelcomeViewController()
    }
    
    private func setupAppearance(){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()    }
}

